import {ADD_POPDUCT,DELETE_POPDUCT,UPDATE_POPDUCT} from "../actions/Cart_actions";
//初始化radux的State数据源
//initial
let inittialState={
    productArray:[
        {id:1,name:"猪肉铺",price:20000,count:10},
        {id:2,name:"老年保暖裤",price:199,count:2},
        {id:3,name:"旺仔牛皮糖",price: 90,count:3},
    ]
};
export default function (state=inittialState,action){
    switch (action.type) {
        case ADD_POPDUCT:
            return {
                productArray:[...state.productArray, action.product]
            }
            break;
        case DELETE_POPDUCT:
            return {
                productArray: state.productArray.filter(item=>{
                    return item.id!=action.product
                })
            }
            break;
        case UPDATE_POPDUCT:
            return  {
                productArray: state.productArray.map(item=>{
                    return item.id==action.product.id ? action.product : item
                })
            }
            break;
        default:
            return state


    }
}